﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IQ.DbBase;
using IQ.BaseWindows;
using System.Data.Common;
using System.Data.Odbc;

namespace IQ.DbA
{
    public partial class FrmGenericQry : Form
    {
        #region # Constructor #
        public FrmGenericQry()
        {
            InitializeComponent();
        }
		#endregion
		
		#region - Private variables - 
        private OdbcConnection connection;
        private DataTable dtResult = new DataTable();
        #endregion 

		#region - Private methods - 
		#endregion 
		
		//#region - Public properties / variables - 
		//#endregion

		//#region - Public methods - 
		//#endregion
		
		#region - Bind Methods - 
		#endregion
		
		#region - Form Events -
		// This region is for event handler methods that are called without the user knowing about it.
		// Such as the page loading.
		#endregion 
		
		#region - Control Methods -
		// This region is for event handler methods that are called knowingly by the user.
		// Such as a button being clicked.
        private void btnConnect_Click(object sender, EventArgs e)
        {
            connection = new System.Data.Odbc.OdbcConnection();
            connection.ConnectionString = txtConnection.Text;
            connection.Open();
            connection.Close();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            grdResult.DataSource = null;
            dtResult = new DataTable();
            dtResult.TableName = DateTime.Now.Millisecond.ToString();
            OdbcCommand cmd = connection.CreateCommand();
            cmd.CommandText = txtQry.Text;
            DbDataAdapter dap = DbHelper.GetDataAdapter(ConnectionTypes.Odbc);
            dap.SelectCommand = cmd;
            try
            {
                //connection.Open();
                dap.Fill(dtResult);
            }
            catch (Exception err)
            {
                General.MesDlg("Error: " + err.Message, 'E');
            }
            finally
            {
                connection.Close();
            }
            grdResult.DataSource = dtResult;
        }
        #endregion        
    }
}
